#ifndef CAMERA_3D_APP_PLUGIN_H
#define CAMERA_3D_APP_PLUGIN_H

#include <ctkPluginActivator.h>

#include <QObject>
#include <QSharedDataPointer>

#include "Base/Interface/DockWidgetRegister.h"
#include "Base/Interface/Listener.h"
#include "Base/data.h"
#include "PublisherPluginWidget.h"
#include "ctkPluginContext.h"
#include "ctkPluginFramework.h"
#include "service/event/ctkEventAdmin.h"
#include "service/event/ctkEventHandler.h"

class PublisherPlugin : public Listener,
                        public ctkEventHandler,
                        public IDockWidget {
  Q_OBJECT
  Q_INTERFACES(ctkEventHandler)
  Q_INTERFACES(IDockWidget)
  Q_INTERFACES(Listener)
 public:
  explicit PublisherPlugin(ctkPluginContext* context);
  ~PublisherPlugin();

  QWidget* CreateWidget() override;
  void InitWidget() override;
  QString GetDockObjectName() override;
  QString GetDockWidgetName() override;
  Qt::DockWidgetArea GetDockWidgetArea() override;
  QDockWidget::DockWidgetFeatures GetDockFeatures() override;

  void sayHello() override;
  void sayHok() override;

 protected:
  void handleEvent(const ctkEvent& event) override;

  void SendLog(const QString plugin_name, const int level, const QString log);

 private:
  void SendEvent(const QString topic, const ctkDictionary& obj);

 private:
  ctkPluginContext* ctk_plugin_context_;
  PublisherPluginWidget* publisher_plugin_widget_;
};

class Camera3DAppActivator : public QObject, public ctkPluginActivator {
  Q_OBJECT
  Q_INTERFACES(ctkPluginActivator)
  Q_PLUGIN_METADATA(IID "EventPublisherPlugin")
  // 向Qt的插件框架声明，希望将xxx插件放入到框架中。

 public:
  void start(ctkPluginContext* context) {
    qDebug() << "start EventPublisherPlugin ----------";
    publisher_plugin_ = new PublisherPlugin(context);

    ctkDictionary dict;
    context->registerService<IDockWidget>(publisher_plugin_, dict);
    context->registerService<Listener>(publisher_plugin_, dict);
  }
  void stop(ctkPluginContext* context) {
    if (publisher_plugin_) {
      delete publisher_plugin_;
      publisher_plugin_ = nullptr;
    }
  }

 private:
  PublisherPlugin* publisher_plugin_{nullptr};
};

#endif  // ROBOT_APP_PLUGIN_H
